package com.mantou.scucorse.core.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mantou.scucorse.core.entity.User;
import com.mantou.scucorse.core.repository.UserRepository;
import com.mantou.scucorse.core.service.UserService;
import com.mantou.scucorse.utils.SystemInfo;
import com.mantou.scucorse.utils.UserSatus;

@Service
public class UserService {


	@Resource
	private UserRepository userRepository;

	@Transactional
	public int create(User user) {
		return userRepository.insert(user);
	}

	@Transactional
	public int createSelective(User user) {
		User queryUser = new User();
		queryUser.setStudentnumber(user.getStudentnumber());
		
		List<User> users = userRepository.selectPage(queryUser, new PageRequest(0,1));
		if(users.size() >0){
			User realuser = users.get(0);
			if(realuser.getStatus() == UserSatus.PASSWORDERROR.getStatus()){
				realuser.setPassword(user.getPassword());
				userRepository.updateByPrimaryKey(realuser);
			}
			user.setId(realuser.getId());
			user.setFailedtimes(realuser.getFailedtimes());
			user.setPassword(realuser.getPassword());
			user.setProxyid(realuser.getProxyid());
			user.setStatus(realuser.getStatus());
			user.setStudentname(realuser.getStudentname());
			user.setStudentnumber(realuser.getStudentnumber());
			return realuser.getId();
		}
		user.setFailedtimes(0);
		user.setStatus(UserSatus.NEW.getStatus());
		userRepository.insertSelective(user);
		return user.getId();
	}

	@Transactional
	public int deleteByPrimaryKey(Integer id) {
		return userRepository.deleteByPrimaryKey(id);
	}

	@Transactional(readOnly = true)
	public User findByPrimaryKey(Integer id) {
		User user = userRepository.selectByPrimaryKey(id);
		return user;
	}

	@Transactional(readOnly = true)
	public int selectCount(User user) {
		return userRepository.selectCount(user);
	}

	@Transactional
	public int updateByPrimaryKey(User user) {
		return userRepository.updateByPrimaryKey(user);
	}
	
	@Transactional
	public int updateByPrimaryKeySelective(User user) {
		return userRepository.updateByPrimaryKeySelective(user);
	}

	@Transactional
	public User findRadomByCourse(int scuCourseID) {
		// TODO Auto-generated method stub
		User ret =  userRepository.findRadomByCourseID(scuCourseID);
		if(ret == null){
			ret = userRepository.findRadom();
		}
		return ret;
	}

	public User getByStudentNumber(String StudentNumber){
		User queryUser = new User();
		queryUser.setStudentnumber(StudentNumber);
		List<User> retUsers = userRepository.selectPage(queryUser, new PageRequest(0,1));
		if(retUsers.size() > 0){
			return retUsers.get(0);
		}
		return null;
	}

	public List<User> find2Auth() {
		// TODO Auto-generated method stub
		User queryUser = new User();
		queryUser.setStatus(UserSatus.NEW.getStatus());
		List<User> rets ;
		rets = userRepository.selectPage(queryUser, new PageRequest(0,SystemInfo.batchNumber));
		int retnum = rets.size();
		if(retnum >= SystemInfo.batchNumber){
			return rets;
		}
		queryUser.setStatus(UserSatus.PASSWORDERROR.getStatus());
		rets.addAll(userRepository.selectPage(queryUser, new PageRequest(0,SystemInfo.batchNumber)));
		return rets;
	}
}
